package com.example.hw2;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.app.DirectAction;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.ArrayList;

/**
 *
 *
 * android 如何用代码给Spinner添加选项
 *
 * 在Android中，你可以通过以下步骤使用代码给Spinner添加选项：
 *
 * 首先，为你想要添加选项的Spinner创建一个数组或列表。可以使用数组或ArrayList来存储选项的字符串数组或列表。
 *
 * javaString[] options = new String[] { "Option 1", "Option 2", "Option 3" };
 *
 * 或者
 * javaArrayList<String> options = new ArrayList<String>();
 * options.add("Option 1");
 * options.add("Option 2");
 * options.add("Option 3");
 *
 *
 * 接下来，创建一个ArrayAdapter并将其关联到Spinner。
 * 使用ArrayAdapter.createFromResource()方法来从字符串资源创建ArrayAdapter，
 * 或者使用ArrayAdapter.createFromEntry()方法来从Spinner选项的字符串数组创建ArrayAdapter。
 *
 * javaArrayAdapter<String> adapter =
 * ArrayAdapter.createFromResource(this, R.array.options, android.R.layout.simple_spinner_item);
 * adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
 *
 * 或者
 * javaArrayAdapter<String> adapter =
 * new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, options);
 * adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
 *
 *
 * 最后，将ArrayAdapter关联到Spinner。
 * 使用Spinner的setAdapter()方法将ArrayAdapter关联到Spinner。
 *
 * javaspinner.setAdapter(adapter);
 *
 * 完成以上步骤后，Spinner就会显示你添加的选项列表供用户选择。
 *
 */
public class MainActivity extends AppCompatActivity {


    Spinner spinner;
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        spinner = findViewById(R.id.spinner);
        textView = findViewById(R.id.textview1);

        // 通过代码给下拉选择框添加下拉选项，以下是方法一
        ArrayList<String> options = new ArrayList<String>();
        options.add("Option 1");
        options.add("Option 2");
        options.add("Option 3");

        ArrayAdapter<String> adapter = //
                new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, options);//
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);//
        spinner.setAdapter(adapter);//

        // 方法二，从配置文件中读取数据，然后添加到下拉选择框中
//        ArrayAdapter<CharSequence> fromResource = //
//                ArrayAdapter.createFromResource(this, R.array.planets_array1,//
//                        android.R.layout.simple_spinner_item);//
//        fromResource.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);//
//        spinner.setAdapter(fromResource);//

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Adapter adapter1 = parent.getAdapter();
                Object item = adapter1.getItem(position);
                Toast.makeText(MainActivity.this,position+":"+id+":"+item,Toast.LENGTH_SHORT).show();

                // 获取配置文件中的数组的方法
//                String[] planets = getResources().getStringArray(R.array.planets_array);
//                Toast.makeText(MainActivity.this,position+":"+id+":"+planets[position],Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // 执行当没有任何选项被选中的代码
            }

        });

        Button button = findViewById(R.id.btn1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Object selectedItem = spinner.getSelectedItem();

                Toast.makeText(MainActivity.this,selectedItem.toString(),Toast.LENGTH_SHORT).show();

            }
        });


        EditText edit1 = findViewById(R.id.edt1);
        edit1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                    String s = edit1.getText().toString();
                    if(TextUtils.isEmpty(s) || s.length() > 10){
                        Toast.makeText(MainActivity.this,"长度不合格",Toast.LENGTH_SHORT).show();
                    }


                }
            }
        });


        EditText edt2 = findViewById(R.id.edt2);

        edt2.addTextChangedListener(new TextWatcher() {

            // 文本改变前触发
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                Log.d("edt2", "beforeTextChanged: "+s);
                Log.d("edt2", "beforeTextChanged: "+start);
                Log.d("edt2", "beforeTextChanged: "+count);
                Log.d("edt2", "beforeTextChanged: "+after);
            }

            //

            /**
             * 文本不改变中触发
             * @param s 原来的字符串
             * @param start 开始的索引，从0开始
             * @param before 始终是0？
             * @param count 字符的数量
             */
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.d("edt2", "onTextChanged: "+s);
                Log.d("edt2", "onTextChanged: "+start);
                Log.d("edt2", "onTextChanged: "+before);
                Log.d("edt2", "onTextChanged: "+count);
            }

            // 文本改变后触发
            @Override
            public void afterTextChanged(Editable s) {
                Log.d("edt2", "afterTextChanged: "+s);
            }
        });

        Button button2 = findViewById(R.id.btn2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // 弹出一个提醒对话框
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("标题");
                builder.setMessage("这是一条消息");
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 处理点击事件
                        Toast.makeText(MainActivity.this,"取消按钮被点击了",Toast.LENGTH_SHORT).show();
                    }
                });

                builder.setNeutralButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this,"确定按钮被点击了",Toast.LENGTH_SHORT).show();
                    }
                });
                AlertDialog alertDialog = builder.create();
                alertDialog.show();



            }
        });

        Button button3 = findViewById(R.id.btn3);
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // 弹出一个提醒对话框
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
//                builder.setTitle("标题");
//                builder.setMessage("这是一条消息");
                builder.setItems(new String[]{"选项1", "选项2", "选项3"}, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 处理点击事件
                        switch (which) {
                            case 0:
                                // 点击了 "选项1"
                                Toast.makeText(MainActivity.this,"选项1",Toast.LENGTH_SHORT).show();
                                break;
                            case 1:
                                // 点击了 "选项2"
                                Toast.makeText(MainActivity.this,"选项2",Toast.LENGTH_SHORT).show();
                                break;
                            case 2:
                                // 点击了 "选项3"
                                Toast.makeText(MainActivity.this,"选项3",Toast.LENGTH_SHORT).show();
                                break;
                        }
                    }
                });
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
            }
        });


        Button button4 = findViewById(R.id.btn4);
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // 弹出一个提醒对话框
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setMultiChoiceItems(new String[]{"选项1", "选项2", "选项3"}, new boolean[]{false,false,false}, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        switch (which) {
                            case 0:
                                // 点击了 "选项1"
                                Toast.makeText(MainActivity.this,"选项1",Toast.LENGTH_SHORT).show();
                                break;
                            case 1:
                                // 点击了 "选项2"
                                Toast.makeText(MainActivity.this,"选项2",Toast.LENGTH_SHORT).show();
                                break;
                            case 2:
                                // 点击了 "选项3"
                                Toast.makeText(MainActivity.this,"选项3",Toast.LENGTH_SHORT).show();
                                break;
                        }
                    }
                });
                builder.setNeutralButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
            }
        });



        Button button5 = findViewById(R.id.btn5);
        Button button6 = findViewById(R.id.btn6);
        Button button7 = findViewById(R.id.btn7);

        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//               showDatePickerDialog();

               DatePickerDialog datePickerDialog = new DatePickerDialog(MainActivity.this);
               datePickerDialog.setTitle("选择日期");

               datePickerDialog.setButton(DatePickerDialog.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int which) {
                       DatePicker datePicker = datePickerDialog.getDatePicker();
                       int selectedYear = datePicker.getYear();
                       int selectedMonth = datePicker.getMonth()+1;
                       int selectedDay = datePicker.getDayOfMonth();
                       updateDateDisplay(selectedDay, selectedMonth, selectedYear);
                   }
               });
               datePickerDialog.setButton(DatePickerDialog.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int which) {
                       Toast.makeText(MainActivity.this,"取消操作",Toast.LENGTH_SHORT).show();
                   }
               });
               datePickerDialog.show();
            }
        });

        button6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        textView.setText(hourOfDay + ":" + minute );

                    }
                },17,4,true);

                timePickerDialog.show();

            }
        });

        button7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyCustomDialog dialog = new MyCustomDialog();
                dialog.show(getSupportFragmentManager(), "dialog"); // 显示对话框
            }
        });
    }


    private void showDatePickerDialog() {
        // 创建 DatePickerDialog 实例
        DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                // 获取选定的日期
                int selectedDay = dayOfMonth;
                int selectedMonth = month + 1; // 月份从 0 开始，所以需要加 1
                int selectedYear = year;

                // 更新日期显示
                updateDateDisplay(selectedDay, selectedMonth, selectedYear);
            }
        },  2023, 9, 1); // 设置初始选定的日期为当前日期
        // 显示 DatePickerDialog
        datePickerDialog.show();
    }

    private void updateDateDisplay(int selectedDay, int selectedMonth, int selectedYear) {
        // 这里可以根据需要更新界面上的日期显示，例如更新一个 TextView 的文本内容
         textView.setText(selectedYear + "-" + selectedMonth + "-" + selectedDay);
    }
}